<?php
use yii\bootstrap\ActiveForm;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/daterangepicker/daterangepicker.css" type="text/css">
<style>
    .dataTables_length {
        margin-top: 10px;
    }
    .dataTable>tbody>tr>td {
        vertical-align: middle;
        font-size: 0.8em;
        padding: 8px 18px;
    }
    .dataTable>thead>tr>th {
        vertical-align: middle;
        font-size: 0.8em;
    }
    .source_type_class {
        min-height: 20px;
        padding-left: 20px;
        margin-bottom: 0;
        font-weight: 400;
        cursor: pointer;
    }
    .form-left{
        border-right: 1px solid #dddddd;
    }
    .examine-content{
        width: 1000px;
        margin-left:-300px;
    }
    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    #table_id_example th, td {
        white-space: nowrap;
    }
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <div style="margin-bottom: 5px">
                        订单编号：<input class="depart-input" id="billNo" style="width: 160px" type="text">&nbsp;
                        仓库：<select id="warehouseId" name="warehouseId" class="depart-input" style="width: 200px">
                                <option value="">选择仓库</option>
                                <?php foreach($warehouse as $value) { ?>
                                    <option value="<?= $value['id']?>"><?= $value['warehouseName']?></option>
                                <?php }?>
                        </select>&nbsp;
                        下单时间：<input class="depart-input" id="billDate" style="width: 200px" type="text">&nbsp;
                        <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#billDate').val('')">清空</button>&nbsp;
                    </div>
                    物料名称：<input class="depart-input" id="materielName" style="width: 160px" type="text">&nbsp;
                    门店：<select id="shopId" name="shopId" class="depart-input" style="width: 200px">
                        <option value="">选择门店</option>
                        <?php foreach($shop as $value) { ?>
                            <option value="<?= $value['ShopID']?>"><?= $value['ShopName']?></option>
                        <?php }?>
                    </select>&nbsp;
                    订单状态：
                    <select id="billStatus" class="depart-input" style="width: 160px">
                        <option value="">全部</option>
                        <option value="0">创建</option>
                        <option value="1">审核</option>
                        <option value="2">发货</option>
                        <option value="3">收货</option>
                        <option value="9">作废</option>
                    </select>
                    <button type="button" class="btn btn-primary depart-modal" id="select_button" style="width: 80px; margin-left: 41px">查询</button>&nbsp;
                    <button type="button" class="btn btn-warning depart-modal" id="reset_button" style="width: 80px;">重置</button>
                    <button type="button" class="btn btn-default depart-modal" id="export_button" style="width: 80px;">导出</button>
                </div>
                <div class="box-body">
                    <a type="button" class="btn btn-success depart-modal" href="/bill/detail" style="width: 80px;">添加</a>
                    <a type="button" class="btn btn-default depart-modal" href="javascript:batchDeliver()" style="width: 80px;">批量发货</a>&nbsp;&nbsp;
                    <table id="table_id_example" class="display">
                        <thead>
                        <tr>
                            <th style="width: 10%">操作</th>
                            <th style="width: 9%"><input type="checkbox" id="chAll">订单编号</th>
                            <th style="width: 9%">状态</th>
                            <th style="width: 9%">仓库</th>
                            <th style="width: 9%">门店</th>
                            <th style="width: 9%">下单人</th>
                            <th style="width: 9%">下单时间</th>
                            <th style="width: 9%">审核人</th>
                            <th style="width: 9%">审核时间</th>
                            <th style="width: 9%">数量</th>
                            <th style="width: 9%">金额</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 固定js -->
<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="../static/plugins/moment/moment.min.js"></script>
<script type="text/javascript" src="../static/plugins/daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="../static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script>
    var table = null;
    $(function () {

        $('#billDate').daterangepicker({
            language: 'zh-CN',
            timePicker: false,
            timePickerIncrement: 30,
            format: 'YYYY-MM-DD',
            locale: {
                format: 'YYYY-MM-DD',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            }
        });
        $('#billDate').val('');

        table = $('#table_id_example').DataTable({
            dom: '<"top">rt<"bottom"flip><"clear">',
            language: {
                "sUrl":'../static/plugins/datatables/language.json'
            },
            "searching": false,
            "processing": true,
            "serverSide": true,
            "scrollY": "550px",
            "scrollCollapse": true,
            "scrollX": true,
            ajax:{
                url:'/bill/get-list',
                type:'post',
                data:function(d){
                    d.billNo = $('#billNo').val();
                    d.materielName = $('#materielName').val();
                    d.warehouseId = $('#warehouseId').val();
                    d.billDate = $('#billDate').val();
                    d.billStatus = $('#billStatus').val();
                    d.shopId = $('#shopId').val();
                },
                error:getError
            },
            aoColumns: [
                { "data": "", "bSortable": false},
                { "data": "billNo", "bSortable": false },
                { "data": "billStatus", "bSortable": false },
                { "data": "warehouseName", "bSortable":false },
                { "data": "ShopName", "bSortable":false },
                { "data": "userId", "bSortable":false },
                { "data": "createTime", "bSortable":false },
                { "data": "reviewer", "bSortable":false },
                { "data": "reviewTime", "bSortable":false },
                { "data": "totalQty", "bSortable":false },
                { "data": "totalAmount", "bSortable":false },
            ],
            columnDefs: [
                {
                    "targets": [0],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        var html = '';

                        if (full.billStatus == '0') {
                            html += '<a href="javascript:audit(\'' + full.billNo + '\')">审核</a>';
                            html += '&nbsp;<a href="javascript:editStatus(\'' + full.billNo + '\',9)">作废</a>';
                        }
                        if (full.billStatus == '1') {
                            if (full.ext1 == 1) {
                                html += '<a href="javascript:editStatus(\'' + full.billNo + '\',2)">发货</a>';
                            } else {
                                html += '<a href="javascript:sync(\'' + full.billNo + '\')">同步</a>&nbsp;';
                            }
                            html += '&nbsp;<a href="javascript:editStatus(\'' + full.billNo + '\',9)">作废</a>';
                        }
                        return html;
                    }
                },
                {
                    "targets": [1],
                    "render": function(data, type, full) {
                        var html = '';
                        if (full.billStatus == '1') {
                            html += '<input type="checkbox" name="ckBill" value="' + full.billNo + '" />&nbsp;';
                        }
                        html += '<a href="/bill/detail?billNo=' + data + '">' + data + '</a>';
                        return html;
                    }
                },
                {
                    "targets": [2],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        if(data == '0') return '创建';
                        if(data == '1') return '已审核';
                        if(data == '2') return '已发货';
                        if(data == '3') return '已收货';
                        if(data == '9') return '作废';
                    }
                }
            ],
            order: [[ 1, "desc" ]]
        });

        $("#select_button").on('click', function () {
            table.ajax.reload();
        });

        $('#billDate').datepicker({ language:'zh-CN', format:'yyyy-mm-dd'} );

        //全选
        $('#chAll').on('click', function () {
            if ($(this).is(':checked')) {
                $("input[name='ckBill']").prop("checked", true)
            } else {
                $("input[name='ckBill']").prop("checked", false)
            }
        });

        //重置操作
        $('#reset_button').click(function(){
            $('#billNo').val('');
            $('#billDate').val('');
            $("#billStatus").val('');
            $('#materielName').val('');
            $('#warehouseId').val('');
            $('#shopId').val('');
            table.ajax.reload();
        });
    });

    //导出
    $('#export_button').on('click',function () {

        var billDate = $('#billDate').val();
        if (billDate) {
            var dateArr = billDate.split(' - ');
            var d1 = Date.parse(dateArr[0]);
            var d2 = Date.parse(dateArr[1]);
            var diffDay = Math.floor((d2 - d1) / (24 * 3600 * 1000));
            if (diffDay > 30) {
                $.jBox.error('日期范围不可超出一个月', '错误');
                return;
            }
        } else {
            $.jBox.error('请选择日期', '错误');
            return;
        }

        var params = 'billNo=' + $('#billNo').val();
        params += '&materielName=' + $('#materielName').val();
        params += '&warehouseId=' + $('#warehouseId').val();
        params += '&billDate=' + $('#billDate').val();
        params += '&billStatus=' + $('#billStatus').val();
        params += '&shopId=' + $('#shopId').val();
        params += '&v=' + Math.random();
        $.jBox.tip('正在导出，请勿重复操作', 'success');
        location.href = encodeURI('/bill/export?' + params);
    });

    //修改订单状态
    function editStatus(billNo, status) {

        $.jBox.confirm("请确认操作", "提示", function(v){
            if (v == 'ok'){
                $.post('/bill/edit-bill-status', {billNo:billNo, status:status},
                    function (data) {
                        if(data.code == 200){
                            $.jBox.tip('操作成功', 'success');
                            table.draw(false);
                        }else{
                            $.jBox.error(data.val, '错误');
                        }
                    },
                    'json'
                );
            }else{
                $.jBox.close();
            }
        });
    }

    //批量发货
    function batchDeliver() {

        var ckBill = $("input[name='ckBill']:checked");
        if (!ckBill.length) {
            $.jBox.tip('请选择订单', '错误');
            return;
        }
        var dataNo = [];
        $.each(ckBill, function () {
            dataNo.push($(this).val().toString());
        });

        $.post('/bill/batch-deliver', {dataNo: dataNo},
            function (data) {
                if (data.code == 200) {
                    $.jBox.tip('操作成功', 'success');
                    table.draw(false);
                } else {
                    $.jBox.error(data.val, '错误');
                }
            },
            'json'
        );
    }

    //自动审核
    function audit(billNo) {
        $.post('/bill/audit-bill', {billNo: billNo},
            function (data) {
                if (data.code == 200) {
                    $.jBox.tip('操作成功', 'success');
                    table.draw(false);
                } else {
                    $.jBox.error(data.val, '错误');
                }
            },
            'json'
        );
    }

    //同步订单
    function sync(billNo, status) {
        $.jBox.confirm("请确认操作", "提示", function(v){
            if (v == 'ok'){
                $.post('/bill/sync-jd', {billNo:billNo},
                    function (data) {
                        if(data.code == 200){
                            $.jBox.tip('同步成功', 'success');
                            table.draw(false);
                        }else{
                            $.jBox.error(data.val, '错误');
                        }
                    },
                    'json'
                );
            }else{
                $.jBox.close();
            }
        });
    }
</script>